﻿@using Microsoft.JSInterop


@inject IJSRuntime JSRuntime

<div class="d-flex flex-column h-100 w-100">
    <div class="d-flex flex-row h-100 w-100">
        <div @ondragstart="(e)=>{}" class="d-flex flex-column flex-grow-1 container-space fd-scrollbar">
           @*  <CascadingValue Value="this" Name="Root">
                <ContainerRuning ContainerData="ContainerData" />
            </CascadingValue> *@
        </div>
    </div>
</div>


@code {
    /// <summary>
    /// 跟容器数据
    /// </summary>
    [Parameter][NotNull]
    public ContainerDto? ContainerData { get; set; }
    /// <summary>
    /// 选中的容器
    /// </summary>
    public ContainerDto? SelectedContainer = null;
    /// <summary>
    /// 选中的控件
    /// </summary>
    public ComponentDto? SelectedComponent = null;
    /// <summary>
    /// 工具箱中正在被拖动的项的数据,ondragstart会触发
    /// </summary>
    private PaletteWidgetDto? DraggedPaletteWidget = null;
    /// <summary>
    ///
    /// </summary>
    private ComponentDto? DraggedComponentData = null;
    /// <summary>
    ///
    /// </summary>
    private List<ComponentDto> DraggedComponentOriginRow = null;
    /// <summary>
    ///
    /// </summary>
    private ContainerDto DraggedComponentOriginContainer = null;
    protected override void OnInitialized()
    {
        base.OnInitialized();
        SelectedContainer = ContainerData;
    }
    public async Task SelectContainerAsync(ContainerDto containerData)
    {
        SelectedContainer = containerData;
        SelectedComponent = null;
        await StateHasChangedAsync();
    }
    public async Task<ContainerDto?> GetSelectedContainerAsync()
    {

        return await Task.FromResult(SelectedContainer);
    }
    public bool IsSelectedContainer(ContainerDto containerData)
    {
        return SelectedContainer != null && SelectedContainer == containerData;
    }

    public async Task SelectComponentAsync(ComponentDto componentData)
    {
        SelectedComponent = componentData;
        SelectedContainer = null;
        await StateHasChangedAsync();
    }
    public async Task<ComponentDto?> GetSelectedComponentAsync()
    {
        return await Task.FromResult(SelectedComponent);
    }
    public bool IsSelectedComponent(ComponentDto component)
    {
        return SelectedComponent != null && SelectedComponent == component;
    }
    /// <summary>
    /// 工具箱中的工具被开始拖动的时候会触发,然后赋值给 <see cref="DraggedPaletteWidget"/>
    /// </summary>
    /// <param name="draggedPaletteWidget"></param>
    /// <returns></returns>
    public async Task SetDraggedPaletteWidgetAsync(PaletteWidgetDto draggedPaletteWidget)
    {
        DraggedPaletteWidget = draggedPaletteWidget;
        DraggedComponentData = null;
        DraggedComponentOriginRow = null;
        DraggedComponentOriginContainer = null;
        await Task.CompletedTask;
    }
    /// <summary>
    /// 返回工具箱中正在被拖动的项
    /// </summary>
    /// <returns></returns>
    public async Task<PaletteWidgetDto?> GetDraggedPaletteWidgetAsync()
    {
        return await Task.FromResult(DraggedPaletteWidget);
    }
    public bool IsDraggedItemPaletteWidget()
    {
        return DraggedPaletteWidget != null && DraggedComponentData == null;
    }

    public async Task SetDraggedComponentAsync(ComponentDto draggedComponentData,
        List<ComponentDto> draggedComponentOriginRow, ContainerDto draggedComponentOriginContainer)
    {
        DraggedComponentData = draggedComponentData;
        DraggedComponentOriginRow = draggedComponentOriginRow;
        DraggedComponentOriginContainer = draggedComponentOriginContainer;
        DraggedPaletteWidget = null;
        await Task.CompletedTask;
    }
    public async Task<ComponentDto> GetDraggedComponentDataAsync()
    {
        return await Task.FromResult(DraggedComponentData);
    }
    public async Task<List<ComponentDto>> GetDraggedComponentOriginRowAsync()
    {
        return await Task.FromResult(DraggedComponentOriginRow);
    }
    public async Task<ContainerDto> GetDraggedComponentOriginContainerAsync()
    {
        return await Task.FromResult(DraggedComponentOriginContainer);
    }
    public bool IsDraggedItemComponent()
    {
        return DraggedComponentData != null && DraggedPaletteWidget == null;
    }

    public async Task StateHasChangedAsync()
    {
        StateHasChanged();
        await Task.CompletedTask;
    }

}
